﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            int n;
            Console.Write("Enter n -> ");
            n = Convert.ToInt32(Console.ReadLine());
            permutation(n);
        }
        public static void permutation(int[] mas, int start, int n)//рекурсия
        {

            string s = "";
            if (start == n - 1)
            {
                for (int i = 0; i < n; i++)
                {
                    Console.Write(mas[i] + " ");
                }
                Console.WriteLine();
            }
            else
            {
                for (int i = start; i < n; i++)
                {
                    int tmp = mas[start];
                    mas[start] = mas[i];
                    mas[i] = tmp;
                    permutation(mas, start + 1, n);
                    tmp = mas[start];
                    mas[start] = mas[i];
                    mas[i] = tmp;
                }
            }
            for (int i = 0; i < n; i++)
            {
                s += mas[i].ToString();
            }
            File.WriteAllText(".\\..\\..\\write.txt", s);
        }

        public static void permutation(int n)
        {
            int[] mas = new int[n];
            for (int i = 0; i < n; i++)
            {
                mas[i] = i + 1;
            }
            permutation(mas, 0, n);          
            
        }
    }
}
